package edu.msse.signup.ui;
import java.util.List;

import javax.faces.context.FacesContext;

import edu.msse.signup.domain.Users;
import edu.msse.signup.service.UsersService;
public class loginHandler {
 String username;
 String password;
 boolean loggedIn=false;
 UsersService userService;
 private ApplicationService applicationService;
 public static String CURRENT_USER = "currentUser";
 
public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
public UsersService getUserService() {
	return userService;
}
public void setUserService(UsersService userService) {
	this.userService = userService;
}
 
public String authenticate(){
	loggedIn=userService.authenticateUser(this.username, this.password);
	
	if (loggedIn){
		List<Users> u=this.userService.findUsersByEmailAddress(getUsername());
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(CURRENT_USER, u.get(0));
	
		return "success";
	}	
	else
	{
		if(applicationService ==null)
		{
			System.out.print("application service is null");
		}
		else
		{
		applicationService.addErrorMessage("loginUser:name","Invalid login. Try again. ");
		}
		return null;
	}
    
		 
}
public boolean isLoggedIn() {
	return loggedIn;
}
public void setLoggedIn(boolean loggedIn) {
	this.loggedIn = loggedIn;
}
public ApplicationService getApplicationService() {
	return applicationService;
}
public void setApplicationService(ApplicationService applicationService) {
	this.applicationService = applicationService;
}
 
 
}
